<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/8
 * Time: 14:52
 */

namespace app\applet\model;


use think\Db;

class Address
{

    /**
     * 添加收货地址
     */
    public function addressAdd($data){
        Db::name("member_address")
            ->insert($data);
        return true;
    }

    /**
     * 收货列表
     */
    public function addressList($page,$page_size,$member_id){
        $data=Db::name("member_address")
            ->field('ma_id,is_default,m_name,m_mobile,address_detail,location_area')
            ->where('member_id',$member_id)
            ->page($page,$page_size)
            ->select();

        $total_num = Db::name("member_address")
            ->where('member_id',$member_id)
            ->count();
        if($total_num>0){
            $t_page=ceil($total_num / $page_size);
        }else{
            $t_page=0;
        }
        $list['current_page'] = $page;
        $list['total_page'] =$t_page;
        $list['page_num'] = $page_size;
        $list['total_num'] = $total_num;
        $list['data'] = $data;
        return $list;
    }

    /**
     * 编辑收货地址
     */
    public function addressEdit($data,$id){

        Db::name("member_address")
            ->where('ma_id',$id)
            ->update($data);
        return true;
    }

    /**
     * 删除收货地址
     */
    public function addressDel($id,$member_id){
        Db::name("member_address")
            ->where(['ma_id'=>$id,'member_id'=>$member_id])
            ->delete();
        return true;
    }

    /**
     * 设置默认地址
     */
    public function addressSet($id,$member_id){
        Db::name("member_address")
            ->where('member_id',$member_id)
            ->update(['is_default'=>2]);

        Db::name("member_address")
            ->where(['ma_id'=>$id,'member_id'=>$member_id])
            ->update(['is_default'=>1]);
        return true;
    }

    /**
     * 查询一条收货地址
     */
    public function addressOne($id){
        $data=Db::name('member_address')
            ->field('ma_id,is_default,m_name,m_mobile,address_detail,location_area')
            ->where('ma_id',$id)
            ->find();
        return $data;
    }

}